package org.sdp.pearproject.service.impl;


import org.sdp.pearproject.dto.convert.*;
import org.sdp.pearproject.dao.domain.*;
import org.sdp.pearproject.dto.request.*;
import org.sdp.pearproject.dto.response.*;
import org.sdp.pearproject.dao.mapper.*;
import org.sdp.pearproject.service.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

/**
 * @ClassName: ProjectAuthServiceSuperImpl
 * @Description:
 * @Author: SDP
 * @Date: 2022-07-20
 * @Version: 1.0
 * Table: pear_project_auth
 * Comment:
 * 项目权限表
 */
public class ProjectAuthServiceSuperImpl extends BaseServiceImpl implements ProjectAuthService {
    @Autowired
    protected ProjectAuthMapper projectAuthMapper;

    public static Map<String, String> uniqueFieldMap = initUniqueFieldMap();

    public static Map initUniqueFieldMap() {
        HashMap map = new HashMap<>();
        return map;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer addProjectAuth(ProjectAuthAddRequest request) throws Exception {
        ProjectAuthWithBLOBs daoRequest = new ProjectAuthAddRequestConvert().convert(request);
        fixEqualCondition(request, "project_auth", "add", "", null);
        Integer result = 0;
        try {
            result = projectAuthMapper.insertSelective(daoRequest);
        }catch(Exception ex) {
            ex.printStackTrace();
            throw checkDBDupException(ex, uniqueFieldMap);
        }
        if (result > 0) {
        }
        addHistory(request, "A", "project_auth", "api", result, null);
        return result;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer updateProjectAuth(ProjectAuthUpdateRequest request) throws Exception {
        fixEqualCondition(request, "project_auth", "update", request.getId()+"", null);
        checkInternalId(request, "project_auth", "update", request.getId());
        addHistory(request, "U", "project_auth", "api", request.getId(), null);

        ProjectAuthWithBLOBs daoRequest = new ProjectAuthUpdateRequestConvert().convert(request);
        ProjectAuthExample example = new ProjectAuthExample();
        example.createCriteria().andIdEqualTo(request.getId());
        if (request.getQueryOptions() != null && request.getQueryOptions().containsKey("setnull")) {
            example.setSetNull(JSONObject.toJavaObject(request.getQueryOptions().getJSONObject("setnull"), ProjectAuthExample.CriteriaString.class));
        }
        Integer result = 0;
        try {
            result = projectAuthMapper.updateByExampleSelective(daoRequest, example);
        }catch(Exception ex) {
            ex.printStackTrace();
            throw checkDBDupException(ex, uniqueFieldMap);
        }
        if (result > 0) {
            return result;
        }
        throw createUpdateFailException();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer deleteProjectAuth(ProjectAuthDeleteRequest request) throws Exception {
        Date now = new Date();
        Integer count = 0;

        BaseRequest abstractRequest = new BaseRequest();
        copyProperties(request, abstractRequest);

        for(int index = 0; index < request.getIdList().size(); index++) {
            fixEqualCondition(request, "project_auth", "delete", request.getIdList().get(index)+"", null);
            checkInternalId(request, "project_auth", "delete", request.getIdList().get(index));
            addHistory(abstractRequest, "D", "project_auth", "api", request.getIdList().get(index), null);
            projectAuthMapper.deleteByPrimaryKey(request.getIdList().get(index));
            count++;
        }
        return count;
    }

    @Override
    public ProjectAuthQueryResponse queryProjectAuth(ProjectAuthQueryRequest request) throws Exception {
        ProjectAuthWithBLOBs daoResponse = projectAuthMapper.selectByPrimaryKeyWithBLOBs(request.getId());
        ProjectAuthQueryResponse response = new ProjectAuthQueryResponse();
        copyProperties(daoResponse, response);
        return response;
    }

    @Override
    public List<ProjectAuthQueryResponse> listProjectAuth(ProjectAuthQueryRequest request) throws Exception {
        ProjectAuthWithBLOBs daoRequest = new ProjectAuthWithBLOBs();
        copyProperties(request, daoRequest);
        ProjectAuthExample example;
        try {
            example = ProjectAuthExample.createExample(daoRequest, request.getQueryOptions());
        } catch (Exception ex) {
            ex.printStackTrace();
            throw createFailException(ex);
        }

        List<ProjectAuthWithBLOBs> list;
        try {
            list = projectAuthMapper.selectByExampleWithBLOBs(example);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw createDatabaseFailException(ex);
        }

        List<ProjectAuthQueryResponse> responseList = new ArrayList<>();
        for(ProjectAuthWithBLOBs item : list) {
            ProjectAuthQueryResponse newItem = new ProjectAuthQueryResponse();
            copyProperties(item, newItem);
            responseList.add(newItem);
        }

        return responseList;
    }

    @Override
    public Integer countProjectAuth(ProjectAuthQueryRequest request) throws Exception {
        ProjectAuthWithBLOBs daoRequest = new ProjectAuthWithBLOBs();
        copyProperties(request, daoRequest);
        ProjectAuthExample example;
        try {
            example = ProjectAuthExample.createExample(daoRequest, request.getQueryOptions());
        } catch (Exception ex) {
            ex.printStackTrace();
            throw createFailException(ex);
        }

        Integer count;
        try {
            count = projectAuthMapper.countByExample(example);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw createDatabaseFailException(ex);
        }

        return count;
    }

    @Override
    public BaseStatResponse statProjectAuth(ProjectAuthQueryRequest request) throws Exception {
        throw createFailException("not implement");
    }

}
